package com.yxhl.zoume.utils;

import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * Created by sugarya on 16/5/13.
 */
public class StorageUtils {

    /**
     * 获取手机空闲容量/MB
     *
     * @return
     */
    public static long getSDFreeSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());

        long blockSize;
        long freeBlocks;
        if (Build.VERSION.SDK_INT < 18) {
            //获取单个数据块的大小(Byte)
            blockSize = sf.getBlockSize();
            //空闲的数据块的数量
            freeBlocks = sf.getAvailableBlocks();
        } else {
            blockSize = sf.getBlockSizeLong();
            freeBlocks = sf.getAvailableBlocksLong();
        }

        //return freeBlocks * blockSize;  //单位Byte
        //return (freeBlocks * blockSize)/1024;   //单位KB
        return (freeBlocks * blockSize) / 1024 / 1024; //单位MB
    }

    public static boolean existSDCard() {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

}
